package com.xinchen.ssh.demo.core.entity.page;

import com.xinchen.ssh.demo.utils.SqlUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Description：
 *
 * @author Xin Chen
 * @version 1.0
 * @date 2016/12/15 10:55
 * @see 2016/12/15 10:55 Created
 ****************************************/
public class QueryConditions implements Serializable {
    private static final long serialVersionUID = 1L;
    public static final String EQUAL = "=";
    public static final String NOT_EQUAL = "<>";
    public static final String GREATER_THAN = ">";
    public static final String GREATER_EQUAL = ">=";
    public static final String LESS_THAN = "<";
    public static final String LESS_EQUAL = "<=";
    public static final String INCLUDE = "%value%";
    public static final String LEFT_INCLUDE = "value%";
    public static final String RIGHT_INCLUDE = "%value";
    public static final String ISNULL = "isnull";
    public static final String ISNOTNULL = "isnotnull";
    public static final String ISEMPTY = "isempty";
    public static final String ISNOTEMPTY = "isnotempty";
    private String orderBy = null;
    private String groupBy = null;
    private String otherHql = null;
    private List propertyNames = new ArrayList();
    private List operators = new ArrayList();
    private List values = new ArrayList();
    private String statementId = null;
    private String countStatementId = null;
    private Object paramValue = null;

    public QueryConditions() {
    }

    public void setCondition(String propertyName, String operator, Object value) {
        if(propertyName != null && !"".equals(propertyName.trim())) {
            String tempOperator = operator;
            if(operator == null || "".equals(operator.trim())) {
                tempOperator = "=";
            }

            Object tempValue = value;
            if("isnull".equals(tempOperator) || "isnotnull".equals(tempOperator) || "isempty".equals(tempOperator) || "isnotempty".equals(tempOperator)) {
                tempValue = tempOperator;
            }

            if(tempValue != null) {
                if(!(tempValue instanceof String) || !"".equals(tempValue.toString())) {
                    this.propertyNames.add(propertyName);
                    if(tempOperator.equals("%value%")) {
                        this.operators.add("like");
                        this.values.add("%" + SqlUtil.parseLikeString(tempValue.toString()) + "%");
                    } else if(tempOperator.equals("value%")) {
                        this.operators.add("like");
                        this.values.add(SqlUtil.parseLikeString(tempValue.toString()) + "%");
                    } else if(tempOperator.equals("%value")) {
                        this.operators.add("like");
                        this.values.add("%" + SqlUtil.parseLikeString(tempValue.toString()));
                    } else {
                        this.operators.add(tempOperator);
                        this.values.add(tempValue);
                    }

                }
            }
        }
    }

    public void setConditionEqual(String propertyName, Object value) {
        this.setCondition(propertyName, "=", value);
    }

    public void setConditionNotEqual(String propertyName, Object value) {
        this.setCondition(propertyName, "<>", value);
    }

    public void setConditionGreaterEqual(String propertyName, Object value) {
        this.setCondition(propertyName, ">=", value);
    }

    public void setConditionGreaterThan(String propertyName, Object value) {
        this.setCondition(propertyName, ">", value);
    }

    public void setConditionLessEqual(String propertyName, Object value) {
        this.setCondition(propertyName, "<=", value);
    }

    public void setConditionLessThan(String propertyName, Object value) {
        this.setCondition(propertyName, "<", value);
    }

    public void setConditionLeftInclude(String propertyName, Object value) {
        this.setCondition(propertyName, "value%", value);
    }

    public void setConditionRightInclude(String propertyName, Object value) {
        this.setCondition(propertyName, "%value", value);
    }

    public void setConditionInclude(String propertyName, Object value) {
        this.setCondition(propertyName, "%value%", value);
    }

    public void setConditionIsNull(String propertyName) {
        this.setCondition(propertyName, "isnull", "isnull");
    }

    public void setConditionIsNotNull(String propertyName) {
        this.setCondition(propertyName, "isnotnull", "isnotnull");
    }

    public void setConditionIsEmpty(String propertyName) {
        this.setCondition(propertyName, "isempty", "isempty");
    }

    public void setConditionIsNotEmpty(String propertyName) {
        this.setCondition(propertyName, "isnotempty", "isnotempty");
    }

    public void setParameter(String statementId, String countStatementId, Object paramValue) {
        this.statementId = statementId;
        this.countStatementId = countStatementId;
        this.paramValue = paramValue;
    }

    public String getOrderBy() {
        return this.orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public String getGroupBy() {
        return this.groupBy;
    }

    public void setGroupBy(String groupBy) {
        this.groupBy = groupBy;
    }

    public String getOtherHql() {
        return this.otherHql;
    }

    public void setOtherHql(String otherHql) {
        this.otherHql = otherHql;
    }

    public List getPropertyNames() {
        return this.propertyNames;
    }

    public void setPropertyNames(List propertyNames) {
        this.propertyNames = propertyNames;
    }

    public List getOperators() {
        return this.operators;
    }

    public void setOperators(List operators) {
        this.operators = operators;
    }

    public List getValues() {
        return this.values;
    }

    public void setValues(List values) {
        this.values = values;
    }

    public String getStatementId() {
        return this.statementId;
    }

    public void setStatementId(String statementId) {
        this.statementId = statementId;
    }

    public String getCountStatementId() {
        return this.countStatementId;
    }

    public void setCountStatementId(String countStatementId) {
        this.countStatementId = countStatementId;
    }

    public Object getParamValue() {
        return this.paramValue;
    }

    public void setParamValue(Object paramValue) {
        this.paramValue = paramValue;
    }
}
